import { Query } from '@/utils/Query.js'

export default function (map) {
  const roadList = [
    { where: '车流量 <=750', color: 'rgb(56, 167, 0)' },
    { where: '车流量 >750 and 车流量 <=1000', color: 'rgb(139, 209, 0)' },
    { where: '车流量 >1000 and 车流量 <=1300', color: 'yellow' },
    { where: '车流量 >1300 and 车流量 <=1600', color: 'rgb(255, 127, 0)' },
    { where: '车流量 >1600', color: 'red' },
  ]
  let drawLayer = null
  const showRoad = (map) => {
    roadList.map((item) => {
      let where = item.where
      const service = { docName: 'guanggu', layerId: 1 }
      Query.queryBySql({
        where,
        service,
        callback: (res) => querySuccess(res, item),
      })

      //查询成功回调
      function querySuccess(result, item) {
        //初始化Zondy.Format.PolygonJSON类
        var format = new Zondy.Format.PolygonJSON()
        //将MapGIS要素JSON反序列化为ol.Feature类型数组
        var features = format.read(result)

        //实例化一个矢量图层drawLayerr用于高亮显示结果
        var drawSource = new ol.source.Vector({
          //是否在地图水平坐标轴上重复
          wrapX: false,
        })
        drawSource.addFeatures(features)

        drawLayer = new ol.layer.Vector({
          source: drawSource,
          style: new ol.style.Style({
            //边线样式
            stroke: new ol.style.Stroke({
              color: item.color,
              width: 10,
            }),
          }),
        })
        map.value.addLayer(drawLayer)
      }
    })
  }
  const closeRoad = (map) => {
    map.value.getLayers().array_.splice(5, 5)
    map.value.render()
  }
  return { showRoad, closeRoad }
}
